package com.tjp.subject.infra.basic.utils;

import com.alibaba.druid.filter.config.ConfigTools;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

/**
 * @program: jz-club
 * @description: 数据库密码加解密工具类
 * @author: aaronTang
 * @create: 2024-02-21
 */
public class DruidEncryptUtil {
    private static String privateKey;

    private static String publicKey;

    static {
        try {
            String[] keyPair = ConfigTools.genKeyPair(512);
            privateKey = keyPair[0];
            System.out.println("privateKey:" + privateKey);
            publicKey = keyPair[1];
            System.out.println("publicKey:" + publicKey);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
    }

    public static String encrypt(String text) throws Exception {
        String encrypt = ConfigTools.encrypt(privateKey, text);
        System.out.println("加密后:" + encrypt);
        return encrypt;
    }

    public static String decrypt(String text) throws Exception  {
        String decrypt = ConfigTools.decrypt(publicKey, text);
        return decrypt;
    }

    public static void main(String[] args) throws Exception {
        String encrypt = encrypt("Wing1Q2W#E");
        System.out.println("加密后:" + encrypt);

        String decryptedText = decrypt(encrypt);
        System.out.println("解密后:" + decryptedText);
    }
}
